from numpy.core.function_base import linspace
point_Origin = (0,0)

__author__ = 'SIOM'
import math
import matplotlib.patches as mpatches
import Circle

def point_distance(point_a,point_b):
    result = math.sqrt((point_a[0]-point_b[0])**2+(point_a[0]-point_b[0])**2)
    return result
#x = point_distance(point_a,point_b)
#print("the distance of (0,0) and (1,1) is %f"%x)
def crossed(a,b):
    distance = math.sqrt((a.center[0]-b.center[0])**2+(a.center[1]-b.center[1])**2)
    if  distance < (a.radius+b.radius):
        return True
    else:
        return False

def circle_area(a):
    circle_area = math.pi*a.radius**2
    return circle_area

#print("The area of the circle is %f"%circle_area(a))

def calculate_area(a,b):#need prove,r1,r1,distance between them
    """
    function s = computer_area2(circle1_x,circle1_y,circle2_x,circle2_y,Circle1_R,Circle2_R,B_angle)
    B_angle = B_angle/360*2*pi;
    d_row1 = dis2(0,0,circle1_x,circle1_y);
    d_row2 = dis2(0,0,circle2_x,circle2_y);
    angle = acos(d_row1/Circle1_R*(sin(B_angle/2)));
    A1 = pi*Circle1_R^2;
    s_area = 2*angle/(2*pi)*A1;
    Angle_area = (Circle1_R^2*sin(angle)*cos(angle));
    s = 2*(s_area-Angle_area);
    """
    if crossed(a,b):

        #a2b_angle = (b.angle - a.angle)/180*math.pi#to be hudu
        a2b_angle = math.acos(a.xy[0]*b.xy[0]+a.xy[1]*b.xy[1])/(math.sqrt(a.xy[0]*a.xy[0]+a.xy[1]*a.xy[1])*math.sqrt(b.xy[0]*b.xy[0]+b.xy[1]*b.xy[1]))
        #print("b.angle is {0}. a.angle is {1}. a2b_angle is {2}".format(b.angle,a.angle,a2b_angle))
        d_a1_r = point_distance(point_Origin,a.center)
        #print("d_a1_r is %f"%d_a1_r)
        d_a2_r = point_distance(point_Origin,b.center)
        angle = math.acos(d_a1_r/a.radius*math.sin(a2b_angle/2))#hudu
        print("angle is %f"%angle)
        sector_area = 0.5*(2*angle)*a.radius**2#o my god, the gongshi is wrong!
        #sector_area = 2*angle/(2*math.pi)*(math.pi*a.r**2)
        print("sector_area is %f"%sector_area)
        #triangle_area = 0.5*(a.r**2)*math.sin(2*angle)
        triangle_area = abs(a.radius**2*math.sin(angle)*math.cos(angle))#cos(angle)maybe fushu
        #print("math.cos(angle) is %f"%math.sin(angle))
        print("triangle_area is %f"%triangle_area)
        s = 2*(sector_area-triangle_area)
        print("a.r is %f"%a.r)
        return  s
    else:
        s = 0
        return s

turning_angle = linspace(0,4/180*math.pi,100)
def computer_center(cartesian_x,cartesian_y,relative_r,relative_angle):
    x = cartesian_x + relative_r*math.cos(relative_angle/360*2*math.pi)
    y = cartesian_y + relative_r*math.sin(relative_angle/360*2*math.pi)
    return (x,y)



Circle.DOE.sensor[0].xy = (0,0)

#print("The cross_area of test1 and test2 is %f"%(calculate_area(test1,test2)))